package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.exception.BusinessException;
import com.company.project.entity.*;
import com.company.project.service.*;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import com.company.project.common.utils.DataResult;

import javax.validation.Valid;


/**
 * ${comments}
 *
 * @author wenbin
 * @email *****@mail.com
 * @date 2022-06-06 12:37:51
 */
@Controller
@RequestMapping("/")
public class AAttendRecordController {
    @Autowired
    private AAttendRecordService aAttendRecordService;
    @Autowired
    private AActiveService aActiveService;
    @Autowired
    private AStudentService aStudentService;
    @Autowired
    private AClassService aClassService;
    @Autowired
    private APaidRecordService aPaidRecordService;

    /**
    * 跳转到页面
    */
    @GetMapping("/index/aAttendRecord")
    public String aAttendRecord() {
        return "aattendrecord/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("aAttendRecord/add")
    @RequiresPermissions("aAttendRecord:add")
    @ResponseBody
    public DataResult add(@RequestBody @Valid AAttendRecordEntity aAttendRecord){
        AStudentEntity aStudent = aStudentService.getById(aAttendRecord.getStudentId());
        if(null==aStudent){
            throw new BusinessException("该学生ID不存在");
        }
        AActiveEntity aActive = aActiveService.getById(aAttendRecord.getActiveId());
        if(null==aActive){
            throw new BusinessException("该活动ID不存在");
        }
        //查询该学生在该活动下是否缴费
        LambdaQueryWrapper<APaidRecordEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(APaidRecordEntity::getStudentId, aAttendRecord.getStudentId())
                .eq(APaidRecordEntity::getActiveId, aAttendRecord.getActiveId());
        APaidRecordEntity newAPaidRecord = aPaidRecordService.getOne(queryWrapper);
        if(null==newAPaidRecord){
            throw new BusinessException("该学生还未报名缴费该活动");
        }
        //查询该学生是否已签到
        LambdaQueryWrapper<AAttendRecordEntity> arQueryWrapper = Wrappers.lambdaQuery();
        arQueryWrapper.eq(AAttendRecordEntity::getStudentId, aAttendRecord.getStudentId())
                .eq(AAttendRecordEntity::getActiveId, aAttendRecord.getActiveId());
        AAttendRecordEntity newAttendRecord = aAttendRecordService.getOne(arQueryWrapper);
        if(null!=newAttendRecord){
            throw new BusinessException("该学生已签到，无需重复签到");
        }
        AClassEntity classEntity = aClassService.getById(aStudent.getClassId());
        aAttendRecord.setStudentName(aStudent.getName());
        aAttendRecord.setActiveName(aActive.getName());
        aAttendRecord.setClassName(classEntity.getName());
        aAttendRecordService.save(aAttendRecord);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("aAttendRecord/delete")
    @RequiresPermissions("aAttendRecord:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        aAttendRecordService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("aAttendRecord/update")
    @RequiresPermissions("aAttendRecord:update")
    @ResponseBody
    public DataResult update(@RequestBody @Valid AAttendRecordEntity aAttendRecord){
        AStudentEntity aStudent = aStudentService.getById(aAttendRecord.getStudentId());
        if(null==aStudent){
            throw new BusinessException("该学生ID不存在");
        }
        AActiveEntity aActive = aActiveService.getById(aAttendRecord.getActiveId());
        if(null==aActive){
            throw new BusinessException("该活动ID不存在");
        }
        //查询该学生在该活动下是否缴费
        LambdaQueryWrapper<APaidRecordEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(APaidRecordEntity::getStudentId, aAttendRecord.getStudentId())
                .eq(APaidRecordEntity::getActiveId, aAttendRecord.getActiveId());
        APaidRecordEntity newAPaidRecord = aPaidRecordService.getOne(queryWrapper);
        if(null==newAPaidRecord){
            throw new BusinessException("该学生还未报名缴费该活动");
        }
        //查询该学生是否已签到
        LambdaQueryWrapper<AAttendRecordEntity> arQueryWrapper = Wrappers.lambdaQuery();
        arQueryWrapper.eq(AAttendRecordEntity::getStudentId, aAttendRecord.getStudentId())
                .eq(AAttendRecordEntity::getActiveId, aAttendRecord.getActiveId());
        AAttendRecordEntity newAttendRecord = aAttendRecordService.getOne(arQueryWrapper);
        if(null!=newAttendRecord){
            throw new BusinessException("该学生已签到，无需重复签到");
        }
        AClassEntity classEntity = aClassService.getById(aStudent.getClassId());
        aAttendRecord.setStudentName(aStudent.getName());
        aAttendRecord.setActiveName(aActive.getName());
        aAttendRecord.setClassName(classEntity.getName());
        aAttendRecordService.updateById(aAttendRecord);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("aAttendRecord/listByPage")
    @RequiresPermissions("aAttendRecord:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody AAttendRecordEntity aAttendRecord){
        Page page = new Page(aAttendRecord.getPage(), aAttendRecord.getLimit());
        LambdaQueryWrapper<AAttendRecordEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        queryWrapper.like(StringUtils.isNotBlank(aAttendRecord.getStudentName()),AAttendRecordEntity::getStudentName, aAttendRecord.getStudentName());
        IPage<AAttendRecordEntity> iPage = aAttendRecordService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

}
